clear; clc; close all;

% 参数设置
a_list = linspace(0.8, 1.1, 100); % a 参数范围
q = 1; % 整数阶
b = 1; P = -2; Q = 2;
N_total = 3000; % 轨迹长度

% 多组初始值
init_vals = [
    0.1, 0.5;
   -5.0, 0.5;
    0.1, 0.1;
   -1.0, 0.5
];

colors = {'r', 'b', 'g', 'y'}; % 对应颜色

% 预分配所有结果
X_plot_all = cell(size(init_vals,1), 1);
Y_plot_all = cell(size(init_vals,1), 1);
A_plot_all = cell(size(init_vals,1), 1);

parfor idx_init = 1:size(init_vals,1)
    x0 = init_vals(idx_init, 1);
    y0 = init_vals(idx_init, 2);
    
    X_plot = [];
    Y_plot = [];
    A_plot = [];
    
    for idx_a = 1:length(a_list)
        a = a_list(idx_a);
        [x, y] = SCLMM(q, a, b, P, Q, x0, y0, N_total);
        
        x_data = x(round(N_total/2):end);
        y_data = y(round(N_total/2):end);
        len = length(x_data);
        
        X_plot = [X_plot, x_data];
        Y_plot = [Y_plot, y_data];
        A_plot = [A_plot, a * ones(1, len)];
    end
    
    X_plot_all{idx_init} = X_plot;
    Y_plot_all{idx_init} = Y_plot;
    A_plot_all{idx_init} = A_plot;
end

% 绘图
figure;

% 左图 x(n) 分岔
subplot(1, 2, 1); hold on;
for idx_init = 1:size(init_vals,1)
    scatter(A_plot_all{idx_init}, X_plot_all{idx_init}, 1, colors{idx_init}, 'filled');
end
xlabel('a', 'FontSize', 14);
ylabel('x(n)', 'FontSize', 14);
title('Fig.9(b) 2D-SCLMM 分岔图 x(n)', 'FontSize', 16);
xlim([min(a_list), max(a_list)]);
ylim([-1 1]);
grid on;
box on;

% 右图 y(n) 分岔
subplot(1, 2, 2); hold on;
for idx_init = 1:size(init_vals,1)
    scatter(A_plot_all{idx_init}, Y_plot_all{idx_init}, 1, colors{idx_init}, 'filled');
end
xlabel('a', 'FontSize', 14);
ylabel('y(n)', 'FontSize', 14);
title('Fig.9(c) 2D-SCLMM 分岔图 y(n)', 'FontSize', 16);
xlim([min(a_list), max(a_list)]);
ylim([-1 1]);
grid on;
box on;
